package com.sea.supply.service.impl;



import com.codecrane.core.util.DateUtils;
import com.codecrane.core.web.BatchImportUtils;
import com.sea.finance.entity.Inventory;
import com.sea.finance.entity.PaymentAgain;
import com.sea.finance.entity.PaymentIncreaseLoss;
import com.sea.finance.service.PaymentIncreaseLossService;
import com.sea.supply.entity.Produce;
import com.sea.supply.entity.ProducePayment;
import com.sea.supply.mapper.ProducePaymentMapper;
import com.sea.supply.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @author 陈鸿飞
 */
@Service
public class ProducePaymentServiceImpl implements ProducePaymentService {

    @Autowired
    private ProducePaymentMapper producePaymentMapper;
    @Autowired
    private PaymentIncreaseLossService paymentIncreaseLossService;

    @Autowired
    private ProduceService produceService;




    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(ProducePayment producePayment) {
        if(producePayment.getProducePayment()==0.00 && !producePayment.getProduceRemark().equals("付款")){
            producePayment.setFlag(1);
            producePayment.setProducePayTime(DateUtils.dateToString(new Date(),"yyyy-MM-dd"));

        }else{
            producePayment.setFlag(0);
        }
        //获取当前最新付款信息
        Integer produceId = producePayment.getProduceId();
        Produce produce=new Produce();
        produce.setId(produceId);
        List<Produce> list = produceService.findByPayment(produce);
        if(null==list || list.size()==0){

        }else{
            //校验新增付款金额不能超过总金额
            Produce pro = list.get(0);
//            if((producePayment.getProducePayment()+pro.getProducePay())>pro.getProduceTotalMoney()){
//                throw new RuntimeException("付款金额不能超过总金额!");
//            }
            if(producePayment.getProduceIsCheck().equals("否")){
                producePayment.setProduceCheckMoney(0.00);
            }else{
                if(producePayment.getProduceCheckMoney()>pro.getProduceWaitPay()){
                    throw new RuntimeException("核销金额不能超过待核销金额!"+pro.getProduceNo());
                }
            }
        }
        if(null==producePayment.getProduceUnusualMoney()){
            producePayment.setProduceUnusualMoney(0.00);
        }
        return producePaymentMapper.insert(producePayment);
    }

    @Override
    public List<ProducePayment> findByProduceId(Integer produceId) {
        return producePaymentMapper.findByProduceId(produceId);
    }

    @Override
    public List<ProducePayment> findByCnd(ProducePayment producePayment) {
        return producePaymentMapper.findByCnd(producePayment);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int usave(ProducePayment producePayment) {
        Integer res=producePaymentMapper.update(producePayment);
        if(StringUtils.isNotBlank(producePayment.getProducePayTime())) {
            paymentIncreaseLossService.updateDetail(producePayment.getId(), producePayment.getProducePayTime(), producePayment.getProducePay());
        }
        return res;
    }

    @Override
    public ProducePayment findById(Integer id) {
        return producePaymentMapper.findById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importPayment(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<ProducePayment> list = BatchImportUtils.iteratorForPaymentDetail(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少新增一条明细！");
            } else {
                for (ProducePayment payment : list) {
                    String produceNo = payment.getProduceNo();
                    Produce produce = produceService.findIdByPo(produceNo);
                    if(null==produce){
                        throw new RuntimeException("暂未查询到PO！【"+produceNo+"】");
                    }
                    payment.setProduceId(produce.getId());
                    payment.setProducePay(payment.getProducePayment()-payment.getProduceCheckMoney()-payment.getProduceUnusualMoney());
                    payment.setProduceRemark("付款");
                    payment.setFlag(0);
                    save(payment);
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void pay(Integer[] payIds, String producePayTime,String produceFile) {
        for(Integer id:payIds){
            ProducePayment payment = findById(id);
            payment.setFlag(1);
            if(null==payment.getProduceUnusualMoney()){
                payment.setProduceUnusualMoney(0.00);
            }
            payment.setProducePay(payment.getProducePayment()-payment.getProduceCheckMoney()-payment.getProduceUnusualMoney());
            payment.setProducePayTime(producePayTime);
            payment.setProduceFile(produceFile);

            usave(payment);
            ProducePayment p = findById(id);
            if(StringUtils.isNotBlank(p.getProducePayTime()) && StringUtils.isNotBlank(p.getProduceFile())){
                p.setProducePaymentType(0);
                producePaymentMapper.update(p);
            }
        }

    }

    @Override
    public Integer delete(Integer id) {
        ProducePayment pro = producePaymentMapper.findById(id);
        if(pro.getProduceRemark().equals("预付款")){
            produceService.updateReadyPayment(pro.getProduceId());
        }
        return producePaymentMapper.delete(id);
    }

    @Override
    public Integer updateBillStatus(String createPlanNo) {
        return producePaymentMapper.updateBillStatus(createPlanNo);
    }

    @Override
    public ProducePayment findByProducePayment(ProducePayment producePayment) {
        return producePaymentMapper.findByProducePayment(producePayment);
    }
}
